<template>
  <div class="lanjian-content" style="height: 760px;overflow: auto">
    <a-form-model
      ref="ruleForm1"
      :labelCol="labelCol"
      :wrapperCol="wrapperCol"
      :rules="rules"
      class='formbox cusFormBox'
      :model="ruleForm"
    >
      <a-form-model-item
        class='formtabMax upload'
      >
        <a-upload
          :multiple="false"
          :file-list.sync="fileList"
          :defaultFileList="ruleForm.fileList"
          :customRequest="customUpload"
          :before-upload="beforeFileUpload"
          :showUploadList="true"
          :remove="handleRemove"
          maxCount="1"
          @preview="downlocal"
          accept=".doc,.docx,.pdf,.xls,.xlsx,.jpg,.png,.jpeg,.txt,.et"
        ><span style="color: #000000d9;font-size: 18px;font-weight: 700; margin-right: 10px"><span
          style="color: #ff5500;font-size: 18px;">*</span>整改报告</span>
          <span><a-icon type="exclamation-circle" class="cio" />仅支持doc、docx、pdf、xls、xlsx、jpg、png、jpeg、txt、et文件格式，文件大小不超过200M。<br></span>
          <a-button class="download" @click.stop="downtmp(1)" type="default" title="下载模板">
            <span class='icon iconfont'>&#xe6c0;</span>
          </a-button>
          <a-divider type="vertical" />
          <a-button>
            <span class='icon iconfont' title="上传文件">&#xe677;</span>
          </a-button>
        </a-upload>
      </a-form-model-item>
      <a-form-model-item
        label='编辑人'
        class='formtab'
        prop='editor'
      >
        <a-input
          v-model.trim='ruleForm.editor'
          placeholder='请输入编辑人'
          :maxLength=50
        />
      </a-form-model-item>
      <a-form-model-item
        label='联系电话'
        class='formtab'
        prop='phone'
      >
        <a-input
          v-model.trim='ruleForm.phone'
          placeholder='请输入联系电话'
          :maxLength=50
        />

      </a-form-model-item>
      <div class="save">
        <a-button type="primary" @click="save" v-show="m_status!='2'&&m_status!='5'">保存</a-button>
      </div>
    </a-form-model>
  </div>
</template>
<script>
import {
  getuploadFileInfo,
  getRectificationReportTotal, addRectificationReportTotal, streamToBuffer, getJLID
} from '@/utils/JsonTools';
import UploadPlus from '@/components/UploadPlus/index.vue';
import {downLoadFunction, getLDType} from "@/utils/common";
import {getTemplete} from "@/utils/fileUtils";
import fs from "fs";

export default {
  name: 'Total',
  components: { UploadPlus },
  data() {
    return {
      m_status:"",
      mulite:false,
      // 表单标题内容宽度设置
      labelCol: { span: 24 },
      wrapperCol: { span: 24},
      ruleForm: {fileList:[]},
      // 表单规则
      rules: {
        fileList: [{ required: true, message: '请上传文件', trigger:['blur','change']  }],
        phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
        editor: [{ required: true, message: '请输入编辑人', trigger: 'blur' }],
      },
      fileList: [],
    };
  },
  mounted(){
    this.getInfos()
    this.getstatus()
  },
  methods: {
    // 上传验证
    beforeFileUpload(file) {
      if (file.name.substring(0, file.name.indexOf('.')).length > 200) {
        this.$message.error('上传文件名字不得超过200字');
        return false;
      }
      let isLt2M = file.size / 1024 / 1024;
      if (isLt2M > 200) {
        this.$message.error('只允许上传200M以下的文件');
        return false;
      }
      let fileExt = file.name.slice(file.name.lastIndexOf('.') + 1);
      let fileTypeArr = ['doc', 'docx', 'pdf', 'xls', 'xlsx', 'jpg', 'png', 'jpeg', 'txt', 'et'];
      if (fileTypeArr.indexOf(fileExt) === -1) {
        this.$message.error('非法的文件类型');
        return false;
      }
      return true;
    },
    handleRemove() {
      this.fileList = [];
    },
    downlocal(file){
      downLoadFunction(file.url,file.name)
    },
    customUpload(file) {
      // beforeFileUpload()返回为true，才会触发这个方法；
      this.handleUpload(file);
    },
    // 构造formData
    handleUpload(file) {
      // const formData = new FormData()
      // let timestamp = new Date().getTime()
      // formData.append('files', file)
      // formData.append('fileUid', file.uid)
      // formData.append('totalBytes', file.size)
      // formData.append('time', timestamp)
      // this.imagerAdd(formData)
      getuploadFileInfo(file.file).then(res => {
        file.onSuccess(res)
        // eslint-disable-next-line no-unused-expressions
        this.fileList = []
        this.fileList.push(res)
        //this.save()
      });
    },
    getInfos() {
      getRectificationReportTotal().then(res => {
        console.log("pp--", res)
        this.ruleForm = res
        this.ruleForm.xcz_yj = getLDType(res.report_xcz_pg_yj)
        this.ruleForm.pzz_yj = getLDType(res.report_pzz_pg_yj)
        this.ruleForm.jdjcs_yj = getLDType(res.report_jdjcs_pg_yj)
        this.ruleForm.dfs_yj = getLDType(res.report_dfs_pg_yj)
        this.ruleForm.swzzb_yj = getLDType(res.report_swzzb_pg_yj)
        this.ruleForm.ld_yj = getLDType(res.ldyj)
        this.fileList = res.fileList
      })
    },
    save () {
      this.$refs.ruleForm1.validate(valid => {
        // eslint-disable-next-line eqeqeq
        if (valid) {
          if (this.fileList.length == 0) {
            this.$message.warn('请上传相关文件')
            return
          }
          this.ruleForm.rep_status = 0
          this.ruleForm.fileList = this.fileList
          addRectificationReportTotal(this.ruleForm).then(res => {
            if (res === 0) {
              this.$message.error('保存失败')
              return
            }
            this.$message.success('保存成功')
          })
        }
      })
    },
    downtmp(type){
      getTemplete().then(res=>{
        const ls= res.filter(function (value) {
          return value.F_MBFL_==(type+"")
        })
        if(ls.length==0||ls[0].paths===undefined){
          this.$message.warn("未查询到模板")
        }else{
          let fileb = fs.createReadStream(ls[0].paths);
          let fsname = ls[0].fname;
          console.log('fsname-----' + fsname);
          streamToBuffer(fileb).then(res => {
            let data = res;
            let contents = new Uint8Array(data.length);
            for (let i = 0; i < data.length; i++) {
              contents[i] = data[i];
            }
            // eslint-disable-next-line no-undef
            let expname = fsname;
            saveAs.saveAs(new Blob([contents], { type: 'application/zip' }), expname);
          });
        }
      })
    },
    getstatus(){
      const plantotal_dbson=JSON.parse(fs.readFileSync("dbson/RectificationReportAllinfos.dbson","utf-8")==""?"[]":fs.readFileSync("dbson/RectificationReportAllinfos.dbson","utf-8"))
      if(plantotal_dbson.length==0){
        this.m_status=="0"
        return;
      }
      let lsarr = plantotal_dbson.filter(function(value){
        //筛选条件
        return value.jlid==getJLID()
      })
      if(lsarr==undefined||lsarr.length==0){
        this.m_status=="0"
        return;
      }
      this.m_status=lsarr[0].f_status

    }
  }
}
</script>
<style scoped lang="less">
.save {
  margin: 40px auto;
}
.upload {
  position: relative;
  /deep/ .ant-upload-list {
    position: absolute;
    left: 100px;
    top: -13px
  }
}
.download{
  background: #fff!important;
  color:#2377ff!important;
  border: 1px solid #2377ff;
}
</style>
<style lang="scss">
// 滚动条背景颜色
.ant-form::-webkit-scrollbar,
.info::-webkit-scrollbar {
  height: 9px;
  width: 9px;
  background-color: #fcfcfc;
}
// 滚动条颜色
.ant-form::-webkit-scrollbar-thumb,
.info::-webkit-scrollbar-thumb {
  border-radius: 9px;
  background-color: #cccccc;
}

.ant-form-item label{
  font-weight: bold;
}

</style>
<style lang="scss" >
// 滚动条背景颜色
.lanjian-content::-webkit-scrollbar,
.info::-webkit-scrollbar {
  height: 9px;
  width: 9px;
  background-color: #fcfcfc;
}

// 滚动条颜色
.lanjian-content::-webkit-scrollbar-thumb,
.info::-webkit-scrollbar-thumb {
  border-radius: 9px;
  background-color: #cccccc;
}

</style>
